/****************************************************
          File Name:33-2.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Fri 29 Aug 2014 08:23:18 PM CST
*****************************************************/

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<signal.h>
#include<stdlib.h>

void dealing (int sig)
{
    if(sig==SIGCHLD)
        printf("thread got SIGCHLD\n");
}

static void
*pthread_fun(void *arg)
{
    struct sigaction act;
    pid_t pid;

    sigemptyset(&act.sa_mask);
    act.sa_handler=dealing;
    act.sa_flags=0;

    if(sigaction(SIGCHLD,&act,NULL)==-1)
    {
        perror("sigaction");
        exit(1);
    }

    switch(pid=fork())
    {
        case -1:
            exit(1);
        case 0:
            raise(SIGCHLD);
            printf("child exited\n");
            break;
        default:
            sleep(3);
            printf("father exited\n");
            break;
    }
}

int main()
{
     pthread_t td;
    void *res;
    if((td=pthread_create(&td,NULL,pthread_fun,NULL))!=0)
    {
        perror("pthread_create");
         exit(1);
    }
    
    sleep(10);
    printf("main thread exiting\n");
    exit(0);
}
